<?php

namespace Modules\User\Http\Controllers\Client\V1;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use Modules\User\Entities\User;
use Modules\User\Entities\SignInRecord;

use Validator;
use ThrowException;
use Auth;
use Setting;

class SignInController extends Controller
{
    // 获取签到列表
    public function index(Request $req)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest();

        $user = AUth::requireLoginUser();

        // 签到记录
        $signRecord = $user->getSignInRecord();

        // 今天是否已签到
        $isSignIn = $signRecord->isSignIn();

        return [
            'list' => $signRecord->days,
            'is_sign_in' => $isSignIn
        ];
    }

    // 签到卡组件获取信息
    public function cardInfo(Request $req)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest();

        $user = AUth::requireLoginUser();

        // 签到记录
        $signRecord = $user->getSignInRecord();

        // 今天是否已签到
        $isSignIn = $signRecord->isSignIn();

        $continuousTimes = 0;
        // 有连续签到
        if ($isSignIn || ($signRecord->last_signin == date('Y-m-d', strtotime('-1 days')))) {
            $continuousTimes = $signRecord->last_continuous_times;
        }

        if ($continuousTimes > 7) {
            $continuousTimes = 7;   // 大于7暂时按7天处理
        }

        $days = [];
        if ($continuousTimes <= 7) {
            $start = 1;
            for ($i=0; $i < 7; $i++) { 
                array_push($days, [
                    'is_completed' => $i <= ($continuousTimes - $start) ? 1 : 0,
                    'score' => SignInRecord::getSignRewardByContinuesTimes($start + $i),
                    'number' => $start + $i
                ]);
            }
        }

        return [
            'is_sign_in' => $isSignIn,
            'sign_in_total' => count($signRecord->days),
            'continuous_times' => $signRecord->last_continuous_times,
            'days' => $days
        ];
    }

    // 获取签到列表
    public function config(Request $req)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest();

        $user = AUth::requireLoginUser();

        $info = [
            'add_reward' => intVal(@Setting::get('sys_signin')['add_reward'] ?: 0),
            'base_reward' => intVal(@Setting::get('sys_signin')['base_reward'] ?: 0),
            'max_reward' => intVal(@Setting::get('sys_signin')['max_reward'] ?: 0),
        ];

        return [
            'info' => $info
        ];
    }

    // 签到
    public function store(Request $req)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest();

        $user = AUth::requireLoginUser();

        // 签到
        list($awardScore, $continuousDays) = $user->signIn();
        
        if ($awardScore) {
            $user->asset('score')->in($awardScore, [
                'description' => '签到奖励'
            ]);
        }

        return [
            'continuous_days' => $continuousDays,
            'award_score' => $awardScore
        ];
    }
}
